package com.jh.util;

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

import javax.imageio.ImageIO;
import javax.imageio.ImageReader;
import javax.imageio.stream.ImageInputStream;
import java.io.*;
import java.util.Iterator;

public class ImgUtil {

    public static void main(String[] args) {
        String s = imgTobase64("F:\\皮卡丘.jpg");
        //String base64Strtp = base64ToImg(s, "base64Strtp","F:\\",  ".jpg");
        //System.out.println(base64Strtp);
    }

    /**
     * 图片转base64字符串
     *
     * @param imgPath
     * @return
     */
    public static String imgTobase64(String imgPath) {
        InputStream inputStream = null;
        byte[] data = null;
        try {
            inputStream = new FileInputStream(imgPath);
            data = new byte[inputStream.available()];
            inputStream.read(data);
            inputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        // 加密
        BASE64Encoder encoder = new BASE64Encoder();
        return encoder.encode(data);
    }

    /**
     * @param imgBase64Str  图片base64字符串
     * @param imgName       图片名称
     * @param saveTopath    要保存到的路径
     * @param imgSuffixName 图片后缀名
     * @return java.lang.String 图片的保存绝对路径
     * @Author lousk
     * @Description 把base64图片转成图片文件并保存
     * @Date 2020/7/31
     * @Time 17:04
     */
    public static String base64ToImg(String imgBase64Str, String imgName, String saveTopath, String imgSuffixName) {

        //创建文件夹
        File dir = new File(saveTopath);
        if (!dir.exists()) {// 判断目录是否存在
            dir.mkdir();
        }
        //========================================
        BASE64Decoder decoder = new BASE64Decoder();
        // 创建流
        OutputStream out = null;
        //Base64解码
        byte[] b = new byte[0];
        //图片完整路径 注意：目录下一定要加一个文件格式不然图片字节输出不了
        String elbowPhotoImgpath = saveTopath + File.separator + imgName + imgSuffixName;
        try {
            b = decoder.decodeBuffer(imgBase64Str);

            for (int i = 0; i < b.length; ++i) {
                if (b[i] < 0) {
                    //调整异常数据
                    b[i] += 256;
                }
            }

            out = new FileOutputStream(elbowPhotoImgpath);
            out.write(b);
            out.flush();

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            // 关闭流
            if (null != out) {
                try {
                    out.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

        }
        return elbowPhotoImgpath;
    }

    public static String getFormatName(byte[] buf) throws IOException {
        InputStream imageStream = new ByteArrayInputStream(buf);

        try (ImageInputStream imageInputStream = ImageIO.createImageInputStream(imageStream)) {
            Iterator<ImageReader> imageReadersList = ImageIO.getImageReaders(imageInputStream);
            if (imageReadersList.hasNext()) {
                ImageReader reader = imageReadersList.next();
                return reader.getFormatName();
            }
        }
        return null;
    }

}
